<?php
/* 
	This file is Copyright Matthew Wilkinson 2009.
	
	This file is part of Oxygen.

	Oxygen is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License version 3 as published by
	the Free Software Foundation at http://www.gnu.org/licenses/gpl-3.0.html

	Oxygen is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

*/
	
	Final Class Lang
		{
		
		private static $constructed = FALSE;
		
		private static $strings = Array();
		
		private static $currentlang = '';
		
		private static $availablelangs = Array();
		
		
		
		private static function _construct()
			{
			if (self::$constructed)
				{
				return;
				}
			else
				{
				self::$constructed = TRUE;
				}
			
			if (Forum::lang() !== null)
				{
				self::changelang(Forum::lang());
				}
			elseif (Forum::SERVER('HTTP_ACCEPT_LANGUAGE') !== null)
				{
				$langs = explode(',', Forum::SERVER('HTTP_ACCEPT_LANGUAGE'));
				$available = Lang::availablelangs();
				foreach ($langs as $lang)
					{
					if (strpos($lang, ';') !== FALSE)
						{
						$lang = substr($lang, 0, strpos($lang, ';'));
						if (array_search($lang, $available) !== FALSE)
							{
							self::changelang($lang);
							break;
							}
						else
							{
							continue;
							}
						}
					}
				}
			else
				{
				self::changelang(_LANG_DEFAULT);
				}
			}
		
		
		public static function text($id)
			{
			self::_construct();
			$parts = explode('/', $id);
			$string = self::$strings;
			for ($n = 0; is_array($string); $n++)
				{
				$string = $string[$parts[$n]];
				}
			return $string;
			}
		
		
		public static function changelang($newlang)
			{
			$newlang = strtolower($newlang);
			if ($newlang !== self::$currentlang)
				{
				$newlang = Security::directory($newlang);
				if (array_search($newlang, scandir(_OXYGEN_LANGS_DIR)) !== FALSE)
					{
					$files = scandir(_OXYGEN_LANGS_DIR . '/' . $newlang);
					foreach ($files as $file)
						{
						if (strpos($file, '.') !== 0 AND strpos($file, '.json') !== FALSE)
							{
							$filestrings = file_get_contents(_OXYGEN_LANGS_DIR . '/' . $newlang . '/' . $file);
							$filestrings = json_decode($filestrings, TRUE);
							self::$strings[substr($file, 0, -5)] = $filestrings;
							}
						}
					self::$currentlang = $newlang;
					}
				else
					{
					throw new InvalidArgumentException('lang/invalid_language');
					}
				}
			}
		
		
		
		public static function availablelangs()
			{
			self::_construct();
			if (count(self::$availablelangs) == 0)
				{
				$dirs = scandir(_OXYGEN_LANGS_DIR);
				foreach ($dirs as $dir)
					{
					if (strpos($dir, '.') === FALSE AND is_dir(_OXYGEN_LANGS_DIR . '/' . $dir))
						{
						self::$availablelangs[] = $dir;
						}
					}
				}
			return self::$availablelangs;
			}
		
		
		public static function currentlang()
			{
			self::_construct();
			return self::$currentlang;
			}
		
		}

?>